<!DOCTYPE html>
<html class="x-admin-sm">
{% load staticfiles %}
{% load my_filters %}
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="{% static 'css/font.css' %}">
    <link rel="stylesheet" href="{% static 'css/xadmin.css' %}">
    <link rel="stylesheet" href="{% static 'css/theme1212.min.css' %}">
    <script type="text/javascript" src="{% static 'lib/layui/layui.js' %}" charset="utf-8"></script>
    <script type="text/javascript" src="{% static 'js/jquery.min.js' %}" charset="utf-8"></script>
    <script type="text/javascript" src="{% static 'lib/jquery.form.js' %}" charset="utf-8"></script>
    <script type="text/javascript" src="{% static 'lib/jquery.cookie.js' %}" charset="utf-8"></script>
    <script type="text/javascript" src="{% static 'js/xadmin.js' %}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]--></head>

<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" id="KanjiaInfo">
            {% csrf_token %}
            <input type="hidden" name="id" value="{{ kanjia.id }}">
            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red">*</span>标题</label>
                <div class="layui-input-inline col-md-6">
                    <input id="title" name="title" value="{{ kanjia.title }}" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red">*</span>活动图片</label>
                <div class="layui-input-inline">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="top_image_btn">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" src="{{ kanjia.get_image_url }}" id="top_image_preview"
                                 style="width: 250px;height: 200px;">
                            <input type="hidden" name="top_image" id="top_image" value="{{ kanjia.top_image }}">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="start_time" class="layui-form-label">
                    <span class="x-red">*</span>开始时间</label>
                <div class="layui-input-inline">
                    <input type="text" id="start_time" name="start_time"
                           value="{{ kanjia.start_time|date:"Y-m-d H:i:s" }}" placeholder="开始时间" lay-verify="required"
                           autocomplete="off"
                           class="layui-input"></div>
                <label for="end_time" class="layui-form-label">
                    <span class="x-red">*</span>结束时间</label>
                <div class="layui-input-inline">
                    <input type="text" id="end_time" name="end_time" value="{{ kanjia.end_time|date:"Y-m-d H:i:s" }}"
                           placeholder="结束时间" lay-verify="required"
                           autocomplete="off"
                           class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label for="price_original" class="layui-form-label">
                    <span class="x-red">*</span>原价(元)</label>
                <div class="layui-input-inline">
                    <input type="text" id="price_original" value="{{ kanjia.price_original|div100 }}"
                           name="price_original" lay-verify="required|number"
                           autocomplete="off" class="layui-input" placeholder="保留2位小数"></div>
                <label for="price_discount" class="layui-form-label">
                    <span class="x-red">*</span>底价(元)</label>
                <div class="layui-input-inline">
                    <input type="text" id="price_discount" value="{{ kanjia.price_discount|div100 }}"
                           name="price_discount" lay-verify="required|number"
                           autocomplete="off" class="layui-input" placeholder="保留2位小数"></div>
            </div>
            <div class="layui-form-item">
                <label for="price_original" class="layui-form-label">
                    <span class="x-red">*</span>最多减价(元)</label>
                <div class="layui-input-inline">
                    <input type="text" id="sub_price_max" value="{{ kanjia.sub_price_max|div100 }}" name="sub_price_max"
                           lay-verify="required|number"
                           autocomplete="off" class="layui-input" placeholder="保留2位小数"></div>
                <label for="sub_price_min" class="layui-form-label">
                    <span class="x-red">*</span>最少减价(元)</label>
                <div class="layui-input-inline">
                    <input type="text" id="sub_price_min" name="sub_price_min" value="{{ kanjia.sub_price_min|div100 }}"
                           lay-verify="required|number"
                           autocomplete="off" class="layui-input" placeholder="保留2位小数"></div>
            </div>
            <div class="layui-form-item">
                <label for="sub_time" class="layui-form-label">
                    <span class="x-red">*</span>减价时间间隔</label>
                <div class="layui-input-inline">
                    <input type="text" id="sub_time" value="{{ kanjia.sub_time }}" name="sub_time"
                           lay-verify="required|number"
                           autocomplete="off" class="layui-input" placeholder="请填写分钟数"></div>
            </div>
            <div class="layui-form-item">
                <label for="music" class="layui-form-label">
                    背景音乐(选填)</label>
                <div class="layui-input-inline">
                    <select class="" lay-filter="music" name="music" id="music">
                        <option value="">请选择</option>
                        {% for music in musics %}
                            <option value="{{ music.id }}"
                                    {% if music.id == kanjia.music_id %}selected{% endif %}>{{ music.path }}-{{ music.name }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>
            <div class="layui-row">
                <label for="desc" class="layui-form-label">
                    <span class="x-red">*</span>活动简介</label>
                <div class="layui-input-inline ">
                    <textarea id="desc" name="desc" style="display: none;">{{ kanjia.desc }}</textarea></div>
            </div>
            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" lay-submit="">保存并预览</button>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'layer', 'laydate', 'upload', 'layedit', 'jquery'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer,
                laydate = layui.laydate,
                upload = layui.upload,
                layedit = layui.layedit;
            ;
            upload.config.data = {
                'csrfmiddlewaretoken': $.cookie("csrftoken")
            }
            upload.config.url = '{% url "common:upload" %}';
            //执行一个laydate实例
            laydate.render({
                elem: '#start_time' //指定元素
                , type: 'datetime'
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end_time' //指定元素
                , type: 'datetime'
            });
            //自定义验证规则
            form.verify({
                nikename: function (value) {
                    if (value.length < 5) {
                        return '昵称至少得5个字符啊';
                    }
                },
                pass: [/(.+){6,12}$/, '密码必须6到12位'],
                repass: function (value) {
                    if ($('#L_pass').val() != $('#L_repass').val()) {
                        return '两次密码不一致';
                    }
                }
            });
            //图片上传
            var uploadInst = upload.render({
                elem: '#top_image_btn'
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#top_image_preview').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    console.log(res)
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $('#top_image').val(res.name)
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
            //富文本编辑器
            layedit.set({
                uploadImage: {
                    url: '{% url "common:upload" %}' //接口url
                    , type: 'post' //默认post
                    , tdata: {
                        'csrfmiddlewaretoken': $.cookie("csrftoken")
                    }
                }
            });
            var desc_t = layedit.build('desc', {
                height: 180,//设置编辑器高度,
                tool: ['strong', 'italic', 'underline', 'del', 'addhr',
                    '|',//分割线
                    'fontFomatt', 'colorpicker', 'left', 'center', 'right',
                    '|',
                    'link', 'unlink', 'image', 'anchors', 'face', 'fullScreen'
                ]
            });
            //监听提交
            form.on('submit(add)',
                function (data) {
                    $('#desc').val(layedit.getContent(desc_t));
                    debugger;
                    $.post("{% url 'wehelp:edit' shop.id %}", $(data.form).serialize(), function (res) {
                            debugger;
                            console.log(res);
                            if (res['status'] == 0) {
                                layer.alert(res.detail, {
                                        icon: 6
                                    },
                                    function () {
                                        xadmin.father_reload();
                                    });
                            } else {
                                alert(res['detail'])
                            }
                        }
                    );
                    return false;
                });
        });
</script>
</body>

</html>